package com.bh.web.controller.dispatch;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.SecurityUtils;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.dispatch.domain.JcDispatch;
import com.bh.dispatch.service.IJcDispatchService;
import com.bh.dispatch.vo.JcDispatchVo;
import com.bh.extend.domain.JcAbnormal;
import com.bh.order.domain.JcLed;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 新提派单Controller
 *
 * @author slx
 * @date 2024-02-20
 */
@RestController
@RequestMapping("/shipment/dispatch")
public class JcDispatchController extends BaseController {
    @Autowired
    private IJcDispatchService jcDispatchService;

    /**
     * 查询新提派单列表
     */
    @ApiOperation("查询新提派单列表")
    @PreAuthorize("@ss.hasPermi('shipment:dispatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcDispatch jcDispatch) {
        startPage();
        jcDispatch.setJcOrganizationId(SecurityUtils.getOrgId());
        List<JcDispatch> list = jcDispatchService.selectJcDispatchList(jcDispatch);
        return getDataTable(list);
    }

    /**
     * 导出新提派单列表
     */
    @PreAuthorize("@ss.hasPermi('shipment:dispatch:export')")
    @Log(title = "新提派单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcDispatch jcDispatch) {
        List<JcDispatch> list = jcDispatchService.selectJcDispatchList(jcDispatch);
        ExcelUtil<JcDispatch> util = new ExcelUtil<JcDispatch>(JcDispatch.class);
        util.exportExcel(response, list, "新提派单数据");
    }

    /**
     * 获取新提派单详细信息
     */
    @ApiOperation("获取新提派单详细信息")
    @PreAuthorize("@ss.hasPermi('shipment:dispatch:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcDispatchService.selectJcDispatchById(id));
    }

    /**
     * 新增新提派单
     */
    @ApiOperation("新增新提派单")
    @PreAuthorize("@ss.hasPermi('shipment:dispatch:add')")
    @Log(title = "新提派单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcDispatchVo jcDispatch) throws Exception {
        //设置组织机构
        if (jcDispatch.getJcOrganizationId() == null) {
            jcDispatch.setJcOrganizationId(getOrgId());
        }
        List<JcLed> leds = jcDispatch.getLeds();
        if (leds != null && !leds.isEmpty()) {
            return error("已经配载订单，不能再次保存");
        }
        return toAjax(jcDispatchService.insertJcDispatch(jcDispatch));
    }

    /**
     * 修改新提派单
     */
    @ApiOperation("修改新提派单")
    @PreAuthorize("@ss.hasPermi('shipment:dispatch:edit')")
    @Log(title = "新提派单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcDispatchVo jcDispatch) throws Exception {
        //设置组织机构
        if (jcDispatch.getJcOrganizationId() == null) {
            jcDispatch.setJcOrganizationId(getOrgId());
        }
        List<JcLed> leds = jcDispatch.getLeds();
        if (leds != null && !leds.isEmpty()) {
            return error("已经配载订单，不能再次保存");
        }
        return toAjax(jcDispatchService.updateJcDispatch(jcDispatch));
    }

    /**
     * 删除新提派单
     */
    @ApiOperation("删除新提派单")
    @PreAuthorize("@ss.hasPermi('shipment:dispatch:remove')")
    @Log(title = "新提派单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jcDispatchService.deleteJcDispatchByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('shipment:dispatch:exe')")
    @ApiOperation("开始执行")
    @PostMapping(value = "/startExe/{ids}")
    public AjaxResult startExe(@PathVariable Long[] ids) {
        return AjaxResult.success(jcDispatchService.startExe(ids));
    }

    @PreAuthorize("@ss.hasPermi('shipment:dispatch:exe')")
    @ApiOperation("结束执行")
    @PostMapping(value = "/endExe/{ids}")
    public AjaxResult endExe(@PathVariable Long[] ids) {
        return AjaxResult.success(jcDispatchService.endExe(ids));
    }


    @PreAuthorize("@ss.hasPermi('shipment:dispatch:abnormal')")
    @ApiOperation("异常修改")
    @PutMapping(value = "/saveAbnormal")
    public AjaxResult saveAbnormal(@RequestBody JcDispatchVo dispatchVo, @RequestBody String abnormal) {
        return AjaxResult.success(jcDispatchService.saveAbnormal(dispatchVo, abnormal));
    }
}
